{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a1e280b5",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "## Python requests 模块\n",
    "### content 作为响应内容\n",
    "* 数据样态？\n",
    "### text检查响应内容\n",
    "* 观察数据？\n",
    "### json()观察结果\n",
    "* 结论？\n",
    "## 实践face++ 人脸检测/face detect\n",
    "* API实践步骤\n",
    "> 1. 导入需要的requests模块\n",
    "> 2. 输入我们需要API网站注册的API_Key\n",
    "> 3. 目标URL[base url]\n",
    "> 4. 沿用API文档的示范代码，准备我们的headers和图片（数据）\n",
    "> 5. 准备symbol？后面的数据，这里需要注意，一定要详细阅读API文档中的“参数功能”，按照要求格式准备payload参数功能有：\n",
    ">> 1. 是否必要？必要的一定要准备好\n",
    ">> 2. 选填的一定是功能，要根据功能需求 好好填！\n",
    "> 6. response响应。注意：\n",
    ">> 1. 详细阅读文档，注意请求格式（GET、POST、DELETE）\n",
    ">> 2. 注意json 和字典的差异，str vs dict\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "89a6dae6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a801b14d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "# base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2.api账户/通行证\n",
    "# API_key = '你的KEY'\n",
    "# API_secret = '你的SECRET'\n",
    "\n",
    "# 3.POST\n",
    "\n",
    "# 4.所有API KEY都可以调用本 API\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API KEY 才能使用，试用 API KEY 无法使用\n",
    "\n",
    "\n",
    "# 5.payload： 酬载，必要阅读api文档中的 *必选项*和*可选性*，及*参数说明*\n",
    "\"\"\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3144ef1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "API_key = 'jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf'\n",
    "API_secret = 'N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR'\n",
    "\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://img0.baidu.com/it/u=4172748064,2888978288&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=755'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e9f9241f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2dce3ede",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647522951,e686711e-11da-4d60-b0f1-a9b209920a9c',\n",
       " 'time_used': 190,\n",
       " 'faces': [{'face_token': 'bb048ee91ce88ff55bdbb669a146edeb',\n",
       "   'face_rectangle': {'top': 242, 'left': 107, 'width': 278, 'height': 278}}],\n",
       " 'image_id': 'AWZTYZ8SZBJxfnNWo/ZA3Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "020a82f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d070fdcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_tokens = face_detect['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1b61036f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bb048ee91ce88ff55bdbb669a146edeb'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d6d622f",
   "metadata": {},
   "source": [
    "# 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64f9e978",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "# Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "# API_key = '你的KEY'\n",
    "# API_sercret = '你的SERCRET'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "\"\"\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':face_tokens,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "# r_analyze = requests.post(url = Analyze_base_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "76b7ed19",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf'\n",
    "API_sercret = 'N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':face_tokens,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e941d7a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f606c73e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 195,\n",
       " 'request_id': '1647523302,cf899d57-1f51-41d0-9b4f-267a1d6802d9',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.007,\n",
       "     'neutral': 99.38,\n",
       "     'disgust': 0.093,\n",
       "     'anger': 0.025,\n",
       "     'surprise': 0.027,\n",
       "     'fear': 0.013,\n",
       "     'happiness': 0.456},\n",
       "    'beauty': {'female_score': 88.082, 'male_score': 85.026},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 18},\n",
       "    'mouthstatus': {'close': 1.192,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.031,\n",
       "     'other_occlusion': 98.777},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 8.032,\n",
       "     'stain': 79.975,\n",
       "     'acne': 55.816,\n",
       "     'health': 12.964},\n",
       "    'headpose': {'yaw_angle': -4.790808,\n",
       "     'pitch_angle': 12.50526,\n",
       "     'roll_angle': 5.095475},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 7.123},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 7.123},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 7.123}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.0},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.005,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.162,\n",
       "      'no_glass_eye_open': 99.27,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.563},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.003,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.997,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 81.49},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.491,\n",
       "      'vector_z_component': 0.967,\n",
       "      'vector_x_component': -0.113,\n",
       "      'vector_y_component': -0.23,\n",
       "      'position_y_coordinate': 0.393},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.514,\n",
       "      'vector_z_component': 0.979,\n",
       "      'vector_x_component': 0.057,\n",
       "      'vector_y_component': -0.197,\n",
       "      'position_y_coordinate': 0.431}}},\n",
       "   'face_rectangle': {'width': 278, 'top': 242, 'left': 107, 'height': 278},\n",
       "   'face_token': 'bb048ee91ce88ff55bdbb669a146edeb'}]}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65806e2a",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ac615fc1",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "API_key = 'jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf'\n",
    "API_secret = 'N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR'\n",
    "\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://img2.baidu.com/it/u=2297031746,3126288543&fm=253&fmt=auto&app=138&f=JPEG?w=362&h=500'    \n",
    "}\n",
    "\n",
    "r_1 = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d4ab3a29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "b4b8b1d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647524291,7d00c030-f7dd-42a1-b3c0-19708d4757ab',\n",
       " 'time_used': 175,\n",
       " 'faces': [{'face_token': '3b11aae468f4921c076e8c62dd16302c',\n",
       "   'face_rectangle': {'top': 194, 'left': 112, 'width': 150, 'height': 150}}],\n",
       " 'image_id': 'lfIoKSYNOj3KEMskwY1Jpg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_1.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "836545e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect1 = r_1.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "23b26b2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_token1 = face_detect1['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "8c840e56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3b11aae468f4921c076e8c62dd16302c'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "593d9317",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "API_key = 'jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf'\n",
    "API_secret = 'N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR'\n",
    "\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://img0.baidu.com/it/u=4022158269,2256394069&fm=253&fmt=auto&app=138&f=JPEG?w=548&h=500'    \n",
    "}\n",
    "\n",
    "r_2 = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b6b1e531",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "49b908a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647524618,b003ef90-ef18-4c8c-9bc2-96c6aec2196d',\n",
       " 'time_used': 167,\n",
       " 'faces': [{'face_token': 'a69dbe9e510a9444382b48f2cc4b9ab1',\n",
       "   'face_rectangle': {'top': 47, 'left': 144, 'width': 267, 'height': 267}}],\n",
       " 'image_id': 'w/7rFcb/BR7f8YvB2M4M1g==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_2.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "eb16e9b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect2 = r_2.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "d2f56c3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_token2 = face_detect2['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "27f70cb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a69dbe9e510a9444382b48f2cc4b9ab1'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "2d720096",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "API_key = 'jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf'\n",
    "API_secret = 'N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_token1':'3b11aae468f4921c076e8c62dd16302c',\n",
    "    'face_token2':'a69dbe9e510a9444382b48f2cc4b9ab1'\n",
    "}\n",
    "\n",
    "r_compare = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "bee9735f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "3971c488",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647526211,f1366edd-15c2-40c2-a939-29c38a9dfea0',\n",
       " 'time_used': 304,\n",
       " 'confidence': 55.188,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_compare.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02733d89",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "faf251b6",
   "metadata": {},
   "source": [
    "* 本周作业\n",
    "* 1.请同学们使用Python函数的方法，封装face++ 人脸检测，人脸分析和人脸对比\n",
    "* 2.请同学们调用face++ 人脸搜索API接口和人脸库API组\n",
    "* 3.尝试应用 上述2，设计宿舍人脸识别系统小应用（微信小程序尝试）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03aea003",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84587029",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
